package com.eostoken.eos_wallet.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseFragment;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.utils.ClipboardUtils;
import com.eostoken.eos_wallet.utils.Constants;
import com.eostoken.eos_wallet.utils.TextviewUtils;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnLongClick;
import party.loveit.bip44forandroidlibrary.core.ECKey;
import party.loveit.bip44forandroidlibrary.utils.Bip44Utils;
import party.loveit.eosforandroidlibrary.Ecc;

import static com.eostoken.eos_wallet.utils.Constants.EXTRA_ACCOUNT_NAME;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_HINT;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_WALLET_PASSWORD;

public class PrivateKeyFragment extends BaseFragment {
    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.createWalletExplainTv)
    AppCompatTextView createWalletExplainTv;
    @BindView(R.id.privateKeyTv)
    AppCompatTextView privateKeyTv;
    @BindView(R.id.importWalletKeyEt)
    AppCompatEditText importWalletKeyEt;
    @BindView(R.id.nextstepBtn)
    QMUIRoundButton nextstepBtn;

    private boolean isBackup = true;
    private List<String> mnemonicCode;
    private String privatekey;
    private String publicKey;
    private String hint;
    private String accountname;
    private String password;

//    private static class LazyHolder {
//        private static final PrivateKeyFragment INSTANCE = new PrivateKeyFragment();
//    }
//
//    public static final PrivateKeyFragment getInstance(String accountname, String password, String hint, List mnemonicCode) {
//        Bundle bundle = new Bundle();
//        bundle.putString(EXTRA_ACCOUNT_NAME, accountname);
//        bundle.putString(EXTRA_WALLET_PASSWORD, password);
//        bundle.putString(EXTRA_HINT, hint);
//        bundle.putStringArrayList(Constants.EXTRA_WALLET_MNEMONIC, (ArrayList<String>) mnemonicCode);
//        LazyHolder.INSTANCE.setArguments(bundle);
//        return LazyHolder.INSTANCE;
//    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_privatekey;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        accountname = bundle.getString(EXTRA_ACCOUNT_NAME, "");
        password = bundle.getString(EXTRA_WALLET_PASSWORD, "");
        hint = bundle.getString(EXTRA_HINT, "");
        mnemonicCode = getArguments().getStringArrayList(Constants.EXTRA_WALLET_MNEMONIC);

        BigInteger pri = Bip44Utils.getPathPrivateKey(mnemonicCode, Constants.EOS_PATH);
        ECKey ecKey = ECKey.fromPrivate(pri);
        privatekey = Ecc.seedPrivate(pri);
        publicKey = Ecc.privateToPublic(privatekey);
        privateKeyTv.setText(privatekey);
    }

    @Override
    protected void initView(View contentView) {
        topbar.setTitle(StringUtils.getString(R.string.str_backup_privatekey));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isBackup) {
                    if (getFragmentManager().getBackStackEntryCount() < 1) {
                        finish();
                    } else {
                        FragmentUtils.pop(getFragmentManager());
                    }
                } else {
                    isBackup = true;
                    verifyPrivateKeyView();
                }

            }
        });
        verifyPrivateKeyView();
    }

    private void verifyPrivateKeyView() {
        String title;
        String explain;
        if (isBackup) {
            title = getString(R.string.str_backup_privatekey);
            explain = getString(R.string.str_backup_privatekey_explain);
        } else {
            title = getString(R.string.str_verification_privatekey);
            explain = getString(R.string.str_verification_privatekey_explain);
        }
        topbar.setTitle(title);
        createWalletExplainTv.setText(explain);
        privateKeyTv.setVisibility(isBackup ? View.VISIBLE : View.GONE);
        importWalletKeyEt.setVisibility(isBackup ? View.GONE : View.VISIBLE);
        createWalletExplainTv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                createWalletExplainTv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                final String newText = TextviewUtils.autoSplitText(createWalletExplainTv, ".");
                if (!TextUtils.isEmpty(newText)) {
                    createWalletExplainTv.setText(newText);
                }
            }
        });
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }

    @OnLongClick({R.id.privateKeyTv})
    public void onViewLongClicked(View view) {
        switch (view.getId()) {
            case R.id.privateKeyTv:
                ClipboardUtils.copyText(privatekey);
                ToastUtils.showShort(StringUtils.getString(R.string.str_copy_success));
                break;
        }
    }

    @OnClick({R.id.nextstepBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.nextstepBtn:
                if (isBackup) {
                    isBackup = false;
                    verifyPrivateKeyView();
                } else {
                    if (importWalletKeyEt.getVisibility() == View.VISIBLE) {
                        String key = importWalletKeyEt.getText().toString().trim();
                        if (ObjectUtils.equals(key, privatekey)) {
//                            MgpWallet mgpWallet = WalletUtils.addWallet(getActivity(), accountList, accountname, password, privatekey, publicKey, mnemonicCode, hint);
//                            FragmentUtils.replace(getFragmentManager(), ActiveFragment.getInstance(mgpWallet), R.id.frag_container, "ActiveFragment", true);
                        } else {
                            ToastUtils.showShort(R.string.str_verification_privatekey_failure);
                        }
                    }
                }
                break;
        }
    }
}
